package com.pshcp.patient.task.job;

import com.external.common.WebServiceException;
import com.external.service.WsRequestService;
import com.pshcp.common.enums.ReCallHisEnum;
import com.pshcp.domain.model.OrderReg;
import com.pshcp.patient.business.OrderRegBusiness;
import com.pshcp.service.OrderRegService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 调用his失败补偿
 *
 * @author ww
 */
@Slf4j
@Component
public class RegOrderRefundRetryJob extends QuartzJobBean {

    @Autowired
    private OrderRegService orderRegService;

    @Autowired
    private OrderRegBusiness orderRegBusiness;

    @Resource
    private WsRequestService wsRequestService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("start RegOrderRefundRetryJob");

        //get cancel reg order
        List<OrderReg> orderRegs = orderRegService.refundRetryList();
        if(CollectionUtils.isEmpty(orderRegs)){
            log.info("refund reg order is null. ignore task.");
            return;
        }
        orderRegs.forEach(o -> {
            try {
                orderRegBusiness.refundRegOrder(o);
                //complete retry
                o.setReCallHis(ReCallHisEnum.OK.getCode());
                orderRegService.update(o);
            } catch (WebServiceException e) {
                log.error("RegOrderRefundRetryJob fail,orderId:{}", o.getId());
            }
        });
        log.info("over RegOrderRefundRetryJob");
    }

}
